<?php

/**
 * Hammer and Nails Software
 * Copyright (c) 2006-2007 Hammer and Nails Software. (http://www.hansoft.org)
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.hansoft.org/licenses/gpl-3.0.txt or through the GNU website:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@hansoft.org so we can send you a copy immediately.
 *
 * @copyright  (c) 2006-2007 Hammer and Nails Software. (http://www.hansoft.org)
 * @license    http://www.hansoft.org/licenses/gpl-3.0.txt
 * @version    $Id: NewsController.php 6 2008-02-17 16:49:51Z philbsd $
 */

class Admin_NewsController extends Hansoft_Controller_Action
{
	protected $_session;

	protected $_tableInstance;

	public function init()
    {		
        $this->_session = new Zend_Session_Namespace(__CLASS__);
		$this->_tableInstance = new Hansoft_Model_News();
    }

	public function indexAction()
	{
		$records = array();		
		$newsRowSet = $this->_tableInstance->fetchAll();
		while ($newsRow = $newsRowSet->current()) {
			$records[] = $newsRow->toArray();
			$newsRowSet->next();
		}
		$this->view->records = $records;		

		if (isset($this->_session->message)) {
			$this->view->message = $this->_session->message;
			unset($this->_session->message);
		}
		$this->render();
	}

	public function addAction()
	{
		if (isset($_POST['btnCancel'])) {
			$this->_redirect('admin/news');
		}
		$newsRow = $this->_tableInstance->createRow();

		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$messages = array();

			$filters = array(
				'title' => 'StringTrim',
				'body' => 'StringTrim',
				);

			$validators = array(
				'title' => array(
					array('StringLength', 1, 255),
					'messages' => array(
						'Title must be between %min% and %max% characters in length'
						)
					),
				'body' => array(
					array('StringLength', 1, 1024),
					'messages' => array(
						'Body must be between %min% and %max% characters in length'
						)
					),
				);

			$input = new Zend_Filter_Input($filters, $validators, $_POST);
		
			if (!$input->isValid()) {
				$messages = $input->getMessages();
			}			

			if (!$messages) {
				$newsRow->title = $input->getUnescaped('title');
				$newsRow->body = $input->getUnescaped('body');
				$newsRow->date_published = date('m/d/Y h:i:s A');
				$auth = Zend_Auth::getInstance();
				$newsRow->published_by = $auth->getIdentity()->username;

				if ($id = $newsRow->save()) {
					$this->_session->message = "News Id#{$id} added successfully";
					$this->_redirect('admin/news');
				} else {
					$messages[] = array('add'=>"Failed to add news.");
				}

	            $this->_redirect('admin/news');
			}
			$this->view->messages = $messages;
		}

		$this->render();
	}
	
	public function editAction()
	{
		if (isset($_POST['btnCancel'])) {
			$this->_redirect('admin/news');
		}

		$params = $this->getRequest()->getUserParams();
		if (!isset($params['id'])) {
			$this->_redirect('admin/news');
		}
		$newsRowSet = $this->_tableInstance->find($params['id']);
		if (!$newsRow = $newsRowSet->current()) {
			$this->_redirect('admin/news');
		}
		$this->view->record = $newsRow->toArray();

		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$messages = array();

			$filters = array(
				'title' => 'StringTrim',
				'body' => 'StringTrim',
				);

			$validators = array(
				'title' => array(
					array('StringLength', 1, 255),
					'messages' => array(
						'Title must be between %min% and %max% characters in length'
						)
					),
				'body' => array(
					array('StringLength', 1, 1024),
					'messages' => array(
						'Body must be between %min% and %max% characters in length'
						)
					),
				);

			$input = new Zend_Filter_Input($filters, $validators, $_POST);
		
			if (!$input->isValid()) {
				$messages = $input->getMessages();
			}			

			if (!$messages) {
				$newsRow->title = $input->getUnescaped('title');
				$newsRow->body = $input->getUnescaped('body');
				$newsRow->date_published = date('m/d/Y h:i:s A');
				$auth = Zend_Auth::getInstance();
				$newsRow->published_by = $auth->getIdentity()->username;

				if ($id = $newsRow->save()) {
					$this->_session->message = "News Id#{$id} added successfully";
					$this->_redirect('admin/news');
				} else {
					$messages[] = array('add'=>"Failed to add news.");
				}

	            $this->_redirect('admin/news');
			}
			$this->view->messages = $messages;
		}

		$this->render();
	}
	
	public function viewAction()
	{
		$params = $this->getRequest()->getUserParams();
		if (!isset($params['id'])) {
			$this->_redirect('admin/news');
		}
		$newsRowSet = $this->_tableInstance->find($params['id']);
		if (!$newsRow = $newsRowSet->current()) {
			$this->_redirect('admin/news');
		}
		$this->view->record = $newsRow->toArray();
		$this->render();
	}

	function deleteAction()
	{
		if (isset($_POST['btnCancel'])) {
			$this->_redirect('admin/news');
		}
		$params = $this->getRequest()->getUserParams();

		if (!isset($params['id'])) {
			$this->_redirect('admin/news');
		}
		$newsRowSet = $this->_tableInstance->find($params['id']);
		$newsRow = $newsRowSet->current();

		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$newsRow->delete();
			$this->_session->message = "News Id#{$params['id']} deleted successfully";
            $this->_redirect('admin/news');
		}

		$this->view->record = $newsRow->toArray();
		$this->render();
	}
}
